package com.qianying.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qianying.comment.CommonResult;  // 修正包路径
import com.qianying.entity.CustomRegionConnect;
import com.qianying.entity.Region;
import com.qianying.mapper.CustomRegionConnectMapper;
import com.qianying.service.CustomRegionConnectService;
import com.qianying.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 客户大区关联服务实现类
 *
 * @author check
 * @date 2023-10-20
 */
@Service
public class CustomRegionConnectServiceImpl implements CustomRegionConnectService {

    @Autowired
    private CustomRegionConnectMapper customRegionConnectMapper;

    @Autowired
    RegionService regionService;

    @Override
    public CommonResult<CustomRegionConnect> getById(Long id) {
        if (id == null) {
            return CommonResult.failed("查询的Id不能为空！");
        }
        CustomRegionConnect entity = customRegionConnectMapper.selectById(id);
        if (entity != null) {
            CommonResult<Map<Long, String>> mapRes = regionService.getRegionMapByCustomerId(entity.getCustomerId());
            if (mapRes.isSuccess()) {
                if (mapRes.getData().containsKey(entity.getRegionId())) {
                    entity.setRegionName(mapRes.getData().get(entity.getRegionId()));
                }
            }
        }else {
            return CommonResult.failed("没有查询到数据！");
        }

        return CommonResult.success(entity);
    }

    @Override
    public CommonResult<CustomRegionConnect> save(CustomRegionConnect entity) {
        if (entity == null) {
            return CommonResult.failed("添加信息对象不能为空！");
        }
        entity.setId(null);
        // 插入数据
        int count = customRegionConnectMapper.insert(entity);
        if (count > 0) {
            // 插入成功后，entity会自动填充主键ID
            return CommonResult.success(entity, "添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    @Override
    public CommonResult<?> updateById(CustomRegionConnect entity) {
        int count = customRegionConnectMapper.updateById(entity);
        return count > 0 ? CommonResult.success("更新成功") : CommonResult.failed("更新失败");
    }

    @Override
    public CommonResult<?> removeById(Long id) {
        int count = customRegionConnectMapper.deleteById(id);
        return count > 0 ? CommonResult.success("删除成功") : CommonResult.failed("删除失败");
    }

    @Override
    public CommonResult<List<CustomRegionConnect>> getByCustomerId(Long customerId) {

        if (customerId == null) {
            return CommonResult.failed("查询id不能为空！");
        }

        List<CustomRegionConnect> list = customRegionConnectMapper.getByCustomerId(customerId);
        if (!list.isEmpty()) {
            CommonResult<Map<Long, String>> mapRes = regionService.getRegionMapByCustomerId(customerId);
            Map<Long, String> map = mapRes.getData();
            for (CustomRegionConnect customRegionConnect : list) {
                if (map.containsKey(customRegionConnect.getRegionId())) {
                    customRegionConnect.setRegionName(map.get(customRegionConnect.getRegionId()));
                }
            }
        }
        return CommonResult.success(list, "获取成功");
    }

}